// 取消关注和关注用户组件
<template>
  <div>
    <van-button
      class="follow-btn"
      type="info"
      color="#3296fa"
      round
      size="small"
      icon="plus"
      v-if="isFollowed"
      @click="onFollow"
      :loading="loading"
      >关注</van-button
    >
    <van-button class="follow-btn" round size="small" v-else @click="onFollow"
      >已关注</van-button
    >
  </div>
</template>

<script>
import { addFollow, delFollow } from '@/api/user.js'

// console.log(addFollow)
// console.log(delFollow)
export default {
  name: 'FollowUser',
  components: {},
  model: {
    prop: 'isFollowed',
    event: 'update-Followed',
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    // 要求用户的id
    aut_id: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      loading: false, // 用来控制按钮的loading状态
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onFollow() {
      // 请求前 打开loading
      this.loading = true
      try {
        if (this.isFollowed) {
          // 执行取消关注
          await delFollow(this.aut_id)
        } else {
          // 执行关注
          await addFollow(this.aut_id)
        }
        // 通知父组件，修改用户的关注状态
        this.$emit('update-Followed', !this.isFollowed)
      } catch (err) {
        //   自己不能关注自己
        let message = '操作失败，请重试'
        if (err.response && err.response.status === 400) {
          message = '你不能关注你自己'
        }
        this.$toast.fail(message)
      }
      this.loading = false // 请求后关闭loading
    },
  },
}
</script>

<style lang="less" scoped></style>
